package com.ktgj.other.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.other.domain.common.HttpTongChengInfoCancel;
import com.ktgj.other.domain.common.HttpTongChengInfoRule;
import com.ktgj.other.domain.vo.ApplyRefundVo;
import com.ktgj.other.domain.vo.CheckoutCounterVo;
import com.ktgj.other.domain.vo.OrderDetailsVo;
import com.ktgj.other.domain.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.other.domain.EtOrder;
import com.ktgj.other.service.IEtOrderService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-07-22
 */
@RestController
@RequestMapping("/order")
public class EtOrderController extends BaseController
{
    @Autowired
    private IEtOrderService etOrderService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "other:order:list")
    @GetMapping("/list")
    public TableDataInfo list(EtOrder etOrder)
    {
        startPage();
        List<EtOrder> list = etOrderService.selectEtOrderList(etOrder);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize(hasPermi = "other:order:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EtOrder etOrder) throws IOException
    {
        List<EtOrder> list = etOrderService.selectEtOrderList(etOrder);
        ExcelUtil<EtOrder> util = new ExcelUtil<EtOrder>(EtOrder.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize(hasPermi = "other:order:query")
    @GetMapping(value = "/{orderid}")
    public AjaxResult getInfo(@PathVariable("orderid") Long orderid)
    {
        return AjaxResult.success(etOrderService.selectEtOrderById(orderid));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "other:order:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EtOrder etOrder)
    {
        return toAjax(etOrderService.insertEtOrder(etOrder));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "other:order:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EtOrder etOrder)
    {
        return toAjax(etOrderService.updateEtOrder(etOrder));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize(hasPermi = "other:order:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderids}")
    public AjaxResult remove(@PathVariable Long[] orderids)
    {
        return toAjax(etOrderService.deleteEtOrderByIds(orderids));
    }

    /**
     * 创建订单
     * */
    @PostMapping("/createOrder")
    public AjaxResult createOrder(HttpServletRequest request, @RequestBody EtOrder etOrder){
        String orderNum;
        try {
            orderNum = etOrderService.createOrder(request, etOrder);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("200", orderNum);
    }

    /**
     * 门票收银台
     * */
    @PostMapping("/checkoutCounter")
    public AjaxResult checkoutCounter(HttpServletRequest request, @RequestBody Map<String, Object> map){
        String orderNum = map.get("orderNum").toString();
        CheckoutCounterVo checkoutCounterVo;
        try {
            checkoutCounterVo = etOrderService.checkoutCounter(request, orderNum);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(checkoutCounterVo);
    }

    /**
     * 支付
     * */
    @PostMapping("/pay")
    public AjaxResult pay(HttpServletRequest request, @RequestBody PayVo payVo){
        String pay;
        try {
            pay = etOrderService.pay(payVo, request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(pay);
    }

    /**
     * 门票小程序支付
     * */
    @PostMapping("/etOrderPay")
    public AjaxResult etOrderPay(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        String orderNumber = map.get("orderNumber").toString();
        String openId = map.get("openId").toString();

        BigDecimal amount = new BigDecimal(0);
        if (map.get("amount") != null && !map.get("amount").equals("")){
            amount = new BigDecimal(map.get("amount").toString());
        }

        String type = map.get("type").toString();

        Map<String, Object> wechatPay;
        try {
            wechatPay = etOrderService.etOrderPay(orderNumber, openId, request, amount, type);
        } catch (Exception e){
            return  AjaxResult.error(e.getMessage());
        }

        if (wechatPay != null)
            return AjaxResult.success(wechatPay);
        else
            return AjaxResult.error("支付失败");
    }

    /**
     * 门票订单列表
     * */
    @PostMapping("/attractionsOrderList")
    public TableDataInfo attractionsOrderList(HttpServletRequest request, @RequestBody Map<String, Object> map){
        String type = EmHelper.OrderStatus.All.getValue();
        if (map.get("type") != null && !map.get("type").equals("")){
            type = map.get("type").toString();
        }
        List<EtOrder> etOrderList;
        try {
            etOrderList = etOrderService.attractionsOrderList(request, type);
        } catch (Exception e){
            return null;
        }

        return getDataTable(etOrderList);
    }

    /**
     * 申请退票
     * */
    @PostMapping("/applyRefund")
    public AjaxResult applyRefund(HttpServletRequest request, @RequestBody ApplyRefundVo applyRefundVo){
        HttpTongChengInfoCancel httpTongChengInfoCancel;
        try {
            httpTongChengInfoCancel = etOrderService.applyRefund(applyRefundVo, request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(httpTongChengInfoCancel);
    }

    /**
     * 订单详情
     * */
    @PostMapping("/orderDetails")
    public AjaxResult orderDetails(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        OrderDetailsVo orderDetailsVo;
        try {
            orderDetailsVo = etOrderService.orderDetails(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(orderDetailsVo);
    }

    /**
     * 退款规则
     * */
    @PostMapping("/refundPolicy")
    public AjaxResult refundPolicy(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        HttpTongChengInfoRule httpTongChengInfoRule = etOrderService.refundPolicy(id);
        return AjaxResult.success(httpTongChengInfoRule);
    }
}
